整合Swagger
依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
配置
@Configuration @EnableOpenApi public class SwaggerConfig { @Bean public Docket docket(Environment environment) { // 仅在测试环境中开启 boolean flag = environment.acceptsProfiles(Profiles.of("dev")); return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()) .enable(flag) .groupName("default") .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { Contact contact = new Contact("陈广生", "https://github.com/rootwhois", "[email protected]"); return new ApiInfo("个人博客管理系统API文档", "永远相信,美好的事情即将发生。", "1.0", null, contact, null, null, new ArrayList<>()); } }
使用
实体类上
@ApiModel(value = "Admin对象", description = "") public class Admin implements Serializable { private static final long serialVersionUID = 1L; @ApiModelProperty("用户id") private Integer userId; }
注解使用:https://www.cnblogs.com/three-fighter/p/12346184.html
文档地址:http://localhost:8080/swagger-ui/index.html
1. 其他
使用Knife4j时报错:
Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPointerException
的解决方法:spring: mvc: pathmatch: matching-strategy: ANT_PATH_MATCHER